package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.constant.UpmpConstant;
import cc.rengu.redp.bizimpl.igas.domain.PTxnCfg;
import cc.rengu.redp.bizimpl.upmp.domain.BProdAuthCtrl;
import cc.rengu.redp.bizimpl.upmp.dao.BProdAuthCtrlMapper;
import cc.rengu.redp.bizimpl.upmp.domain.BProdFeeInfo;
import cc.rengu.redp.bizimpl.upmp.domain.BProdStageInfo;
import cc.rengu.redp.bizimpl.upmp.service.IBProdAuthCtrlService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 刘玉亮.LL
 * @since 2021-06-10
 */
@Service
public class BProdAuthCtrlServiceImpl extends ServiceImpl<BProdAuthCtrlMapper, BProdAuthCtrl> implements IBProdAuthCtrlService {

    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    /**
     * 查询基础产品已选交易
     *
     * @param instId
     * @param prodType
     * @param baseProdCode
     * @return
     */
    @Override
    public List<BProdAuthCtrl> getBaseProdAuthCtrl(String instId, String prodType, String baseProdCode) {
        LambdaQueryWrapper<BProdAuthCtrl> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(instId)) {
            wrapper.eq(BProdAuthCtrl::getInstId, instId);
        }
        if (StringUtils.isNotBlank(prodType)) {
            wrapper.eq(BProdAuthCtrl::getProdType, prodType);
        }
        if (StringUtils.isNotBlank(baseProdCode)) {
            wrapper.eq(BProdAuthCtrl::getBaseProdCode, baseProdCode);
        }
        wrapper.eq(BProdAuthCtrl::getStatus, "01");
        List<BProdAuthCtrl> list = this.list(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BProdAuthCtrl::getInstId, upmpSysParamService.getById(UpmpConstant.IGAS_DEFAULT_INST_ID).getPrimaryValue());
            wrapper.eq(BProdAuthCtrl::getProdType, prodType);
            wrapper.eq(BProdAuthCtrl::getBaseProdCode, baseProdCode);
            wrapper.eq(BProdAuthCtrl::getStatus, "01");
            list = this.list(wrapper);
        }
        return list;
    }

    @Override
    public BProdAuthCtrl findByBProdAuthCtrl(String instId, String prodType, String baseProdCode, String txnNum, String bizType) {
        LambdaQueryWrapper<BProdAuthCtrl> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BProdAuthCtrl::getInstId, instId);
        wrapper.eq(BProdAuthCtrl::getProdType, prodType);
        wrapper.eq(BProdAuthCtrl::getBaseProdCode, baseProdCode);
        wrapper.eq(BProdAuthCtrl::getTxnNum, txnNum);
        wrapper.eq(BProdAuthCtrl::getBizType, bizType);
        wrapper.eq(BProdAuthCtrl::getStatus, "01");
        return this.getOne(wrapper);
    }

    @Override
    public void delete(List<BProdAuthCtrl> list) {
        for (BProdAuthCtrl bProdAuthCtrl : list) {
            this.baseMapper.deleteById(bProdAuthCtrl.getId());
        }
    }

    /**
     * 查询基础产品已选交易
     *
     * @param instId
     * @param prodType
     * @param baseProdCode
     * @return
     */
    @Override
    public List<BProdAuthCtrl> selectBaseProdAuthCtrl(String instId, String prodType, String baseProdCode) {
        LambdaQueryWrapper<BProdAuthCtrl> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BProdAuthCtrl::getInstId, instId);
        wrapper.eq(BProdAuthCtrl::getProdType, prodType);
        wrapper.eq(BProdAuthCtrl::getBaseProdCode, baseProdCode);
        return this.list(wrapper);
    }

    /**
     * 新增时查询标准产品已选交易
     *
     * @param instId
     * @param prodType
     * @param baseProdCode
     * @param prodCode
     * @return
     */
    @Override
    public List<BProdAuthCtrl> getProdAuthCtrlAdd(String instId, String prodType, String baseProdCode, String prodCode) {
        LambdaQueryWrapper<BProdAuthCtrl> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BProdAuthCtrl::getInstId, instId);
        wrapper.eq(BProdAuthCtrl::getProdType, prodType);
        wrapper.eq(BProdAuthCtrl::getBaseProdCode, baseProdCode);
        wrapper.eq(BProdAuthCtrl::getProdCode, prodCode);
        return this.list(wrapper);
    }

    @Override
    public BProdAuthCtrl findByUKAdd(String instId, String prodType, String baseProdCode, String prodCode, String txnNum, String bizType) {
        LambdaQueryWrapper<BProdAuthCtrl> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BProdAuthCtrl::getInstId, instId);
        wrapper.eq(BProdAuthCtrl::getProdType, prodType);
        wrapper.eq(BProdAuthCtrl::getBaseProdCode, baseProdCode);
        wrapper.eq(BProdAuthCtrl::getProdCode, prodCode);
        wrapper.eq(BProdAuthCtrl::getTxnNum, txnNum);
        wrapper.eq(BProdAuthCtrl::getBizType, bizType);
        return this.getOne(wrapper);
    }
}
